package com.wly.datapool.monster
{
	import com.wly.ActiveManager;
	import com.wly.datapool.define.DirDefine;
	import com.wly.datapool.define.GlobalDefine;
	import com.wly.datapool.global.GlobalInfo;
	import com.wly.dynamicc.DynamicManager;
	import com.wly.dynamicc.DynamicObject;
	import com.wly.ui.panel.GlobalInfoPanel;
	import com.wly.ui.sound.SoundLayer;
	import com.wly.utils.Func;
	import com.wly.utils.MyPoint;
	
	import flash.display.MovieClip;

	public class MonsterInfo
	{
		public var monsterBase:MonsterInfoStatic;
		public var startTick:int;
		public var road:Array = [];
		public var hpMax:int;
		private var _hp:int;
		public var speed:int;
		public var isAlive:Boolean = true;
		private var _model:MovieClip;
		public var activeKey:int = -1;
		private var _dynamicObj:DynamicObject;
		private var _curRoadPos:int;
		private var _hpBar:MovieClip;
		
		private var _moveStat:int;
		private const FORWARD:int  = 0;
		private const BACKWARD:int = 1;
		public var deadCheck:Function;
		
		private var _curDir:int = DirDefine.DIR_DOWN;
		
		public function MonsterInfo(baseIndex:int)
		{
			monsterBase = MonsterPool.getInsance().getMonsterInfoByIndex(baseIndex);
			var cls:Class = monsterBase.modelClass;
			_model = new cls;
			_hpBar = new MonsterHpBar;
			//_hpBar.x = -_model.width/2;
			_hpBar.y = -_hpBar.height;
			_model.addChild(_hpBar);
		}
		
		public function start():void
		{
			_dynamicObj = new DynamicObject;
			_dynamicObj.action = onTick;
			
			hp = hpMax;
			var pos:MyPoint = Func.caculateCellPos(startCellIndex);
			
			_model.x = pos.x-_model.width/2;
			_model.y = pos.y-_model.height;
			ActiveManager.getInstance().addMonster(this);
			DynamicManager.getInstance().registerObj(_dynamicObj);
		}
		
		public function get model():MovieClip
		{
			return _model;
		}
		
		public function get x():int
		{
			return _model.x+_model.width/2;
		}
		
		public function get y():int
		{
			return _model.y+_model.height;			
		}
		
		public function destory():void
		{
			DynamicManager.getInstance().unRegisterObj(_dynamicObj);
			ActiveManager.getInstance().removeMonster(this);
		}
		
		public function get name():String
		{
			return monsterBase.name;
		}
		
		public function get startCellIndex():int
		{
			_curRoadPos = 0;
			_moveStat   = FORWARD;
			return road[_curRoadPos] as int;
		}
		
		public function get curCellIndex():int
		{
			return road[_curRoadPos] as int;
		}
		
		public function get targetCellIndex():int
		{
			if(_moveStat == FORWARD)
			{
				if(_curRoadPos+1 < road.length)
				{
					return road[_curRoadPos+1] as int;
				}
				else
				{
					_moveStat = BACKWARD;
				}
			}
			if(_moveStat == BACKWARD)
			{
				if(_curRoadPos-1>=0)
				{
					return road[_curRoadPos-1] as int;
				}
				else
				{
					//已经走完
					return -1;
				}
			}
			return 0;
		}
		
		public function onTick():void
		{
			var targetPos:MyPoint = Func.caculateCellPos(targetCellIndex);
			if(targetCellIndex == -1)
			{
				onMoveComplete();
				return;
			}

			if(this.x==targetPos.x && this.y==targetPos.y)
			{
				if(_moveStat == FORWARD)
				{
					_curRoadPos++;
				}
				else
				{
					_curRoadPos--;
				}
			}
			var dir:int = DirDefine.cacuateDir(Func.caculateCellPos(curCellIndex), Func.caculateCellPos(targetCellIndex));
			switch(dir)
			{
				case DirDefine.DIR_DOWN:
					_model.y += speed;
					this.dir = DirDefine.DIR_DOWN;
					break;
				case DirDefine.DIR_UP:
					_model.y -= speed;
					this.dir = DirDefine.DIR_UP;
					break;
				case DirDefine.DIR_LEFT:
					_model.x -= speed;
					this.dir = DirDefine.DIR_LEFT;
					break;
				case DirDefine.DIR_RIGHT:
					_model.x += speed;
					this.dir = DirDefine.DIR_RIGHT;
					break;
				default:
					break;	
			}
		}
		
		private function onMoveComplete():void
		{
			trace(this, "onMoveComplete", deadCheck);
			destory();
			isAlive = false;
			if(deadCheck != null)
			{
				deadCheck();
			}
			//ActiveManager.getInstance().removeMonster(this);
		}
		
		public function get hp():int
		{
			return _hp;
		}
		
		public function set hp(val:int):void
		{
			_hp = val;
			if(hp==hpMax)
			{
				_hpBar.gotoAndStop(100);
			}
			else
			{
				_hpBar.gotoAndStop(int(hp/hpMax*100+1));
			}
			//trace(this, _hp);
			if(_hp<=0)
			{
				onDead();
			}
		}
		
		private function onDead():void
		{
			if(GlobalInfo.isPlaySound)
			{
				var deadSound:MovieClip = new monsterBase.deadSoundClass;
				var val:int = int(Math.random()*1000)%GlobalDefine.MONSTERDEADSOUNDNUM;
				deadSound.gotoAndStop(val+2);
				SoundLayer.getInstance().addChild(deadSound);
			}
			isAlive = false;
			GlobalInfo.money += monsterBase.money;
			GlobalInfoPanel.getInstance().upodateMoney();
			destory();
			if(deadCheck != null)
			{
				deadCheck();
			}
		}
		
		public function get centerX():int
		{
			return _model.x + _model.width/2;
		}
		
		public function get centerY():int
		{
			return _model.y + _model.height/2;
		}
		
		public function set dir(val:int):void
		{
			if(val != _curDir)
			{
				_curDir = val;
				switch(val)
				{
					case DirDefine.DIR_DOWN:
						if(model)
						{
							model.gotoAndPlay(monsterBase.down);
						}
						break;
					case DirDefine.DIR_LEFT:
						if(model)
						{
							model.gotoAndPlay(monsterBase.left);
						}
						break;				
					case DirDefine.DIR_RIGHT:
						if(model)
						{
							model.gotoAndPlay(monsterBase.right);
						}
						break;				
					case DirDefine.DIR_UP:
						if(model)
						{
							model.gotoAndPlay(monsterBase.up);
						}
						break;
					default:
						break;
				}
			}
		}
	}
}